package oot.activity;

import oot.data.Config;
import oot.data.Connection;
import oot.data.User;
import oot.tab.activity.FragmentTabs;
import oot.tab.activity.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class FlashScreenActivity extends Activity {
	public static User user;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.flash_screen_layout);
		
		
		user= new User("","");
		if (haveNetworkConnection()) {
			
			Log.i("test","has internet");
			SharedPreferences sharedPreferences = getSharedPreferences(
					"login", MODE_PRIVATE);
			Connection.accesstoken =sharedPreferences.getString(Config.ACCESSTOKEN, "err");
			
			//SharedPreferences.Editor editor = sharedPreferences.edit();
			if(!sharedPreferences.getBoolean(Config.LOGEDIN, false)){
				Intent i = new Intent(this,LoginActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
			}
			else{
				Log.i("test"," is login ");
				user.setUserID(sharedPreferences.getString(Config.USER_ID, "-1"));
				user.setUserAvatarURL(sharedPreferences.getString(Config.USER_AVATA_URL, ""));
				user.setAccesstoken(sharedPreferences.getString(Config.ACCESSTOKEN, "-1"));
				user.setUserName(sharedPreferences.getString(Config.USER_NAME, "namedefault"));
				Intent i = new Intent(this,FragmentTabs.class);
				i.putExtra("user", FlashScreenActivity.user);
				//i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
			}
		}
		else {
			Toast toast = Toast.makeText(getApplicationContext(),
					"No internet", Toast.LENGTH_SHORT);
			toast.show();
		}
	}

	private boolean haveNetworkConnection() {
		boolean haveConnectedWifi = false;
		boolean haveConnectedMobile = false;

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] netInfo = cm.getAllNetworkInfo();
		for (NetworkInfo ni : netInfo) {
			if (ni.getTypeName().equalsIgnoreCase("WIFI"))
				if (ni.isConnected())
					haveConnectedWifi = true;
			if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
				if (ni.isConnected())
					haveConnectedMobile = true;
		}
		return haveConnectedWifi || haveConnectedMobile;
	}
}
